package cn.sy.web.controller;

import cn.sy.domain.Bom;
import cn.sy.dto.ProcessDto;
import cn.sy.dto.ProductionPlanDto;
import cn.sy.service.IProductionPlanService;
import cn.sy.domain.ProductionPlan;
import cn.sy.query.ProductionPlanQuery;
import cn.sy.result.JSONResult;
import cn.sy.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/plan")
public class ProductionPlanController {

    @Autowired
    public IProductionPlanService productionPlanService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody ProductionPlan productionPlan){
        if(productionPlan.getId()!=null){
            productionPlanService.updateById(productionPlan);
        }else{
            productionPlanService.insert(productionPlan);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        productionPlanService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(productionPlanService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(productionPlanService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody ProductionPlanQuery query){
        Page<ProductionPlan> page = new Page<ProductionPlan>(query.getPage(),query.getRows());
        page = productionPlanService.selectPage(page);
        return JSONResult.success(new PageList<ProductionPlan>(page.getTotal(),page.getRecords()));
    }

    /**
     * 生产计划上传
     */
    @RequestMapping(value = "/production",method = RequestMethod.POST)
    public JSONResult productionPlan(@Valid @RequestBody ProductionPlanDto productionPlanDto){
        productionPlanService.production(productionPlanDto);
        return JSONResult.success();
    }

    /**
     * 审核失败
     */
    @RequestMapping(value = "/process",method = RequestMethod.POST)
    public JSONResult  processFail(@RequestBody ProcessDto processDto){
        productionPlanService.processFail(processDto);
        return JSONResult.success();
    }

    /**
     * 审核成功
     */
    @RequestMapping(value = "/success",method = RequestMethod.POST)
    public JSONResult  processSuccess(@RequestBody ProcessDto processDto){
        productionPlanService.processSuccess(processDto);
        return JSONResult.success();
    }

    /**
     * bom清单生成
     */
    @RequestMapping(value = "/bom", method = RequestMethod.POST)
    public JSONResult addBom(@RequestBody Bom bom) {
        return productionPlanService.addBom(bom);
    }
}
